module SimpleTabsHelper
  require 'grid_helper.rb'
 
  def simple_tabs
    yield lista = ListTabs.new
    render_tabs(lista)
  end
 
  def render_tabs(lista)
    rtn = []
    rtn << tag_for("tab",  procesar(lista, "tab")  {|llamada, i| link_for(llamada, i) })
    rtn << tag_for("pane", procesar(lista, "pane") {|llamada, i| send(llamada[0], *llamada[2], &llamada[3]) })
    rtn.join("\n")
  end
 
  def tag_for(clase, texto)
    content_tag(:ul, texto, :class => "#{clase}-selector", :id => "#{clase}_control" )
  end
 
  def procesar(lista, tipo)
    rtn = []
    lista.llamadas.each_with_index do |llamada, i|
      options = {:class => "#{tipo}-#{'un' if i > 0}selected", :id => "#{tipo}#{i}"}
      rtn << content_tag(:li, yield(llamada, i), options)
    end
    rtn.join("\n")
  end
 
  def link_for(llamada, i)
    link_to_function llamada[1], "changeTab(#{i});"
  end

  class ListTabs
    attr_reader :llamadas

    def initialize
      @llamadas = []
    end

    def method_missing(method, *args, &block)
      label = args.shift
      @llamadas << [method, label, args, block]
    end
  end
end
ActionView::Base.send :include, SimpleTabsHelper
